﻿@page "/"
@using System.ComponentModel.DataAnnotations;
<Card Bordered="true" Title=@("疫苗列表")>
    <Extra> <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Plus" OnClick="Add">添加疫苗</Button></Extra>
    <Body>
        <Table TItem="VaccineDto" DataSource="@datas">

            <Column @bind-Field="@context.Name"></Column>
            <Column @bind-Field="@context.Descr"></Column>
            <Column @bind-Field="@context.Qty"></Column>

            <ActionColumn Title="操作">
                <Space Size=@("middle")>
                    <Space>
                        <SpaceItem><Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Edit" OnClick="()=>EditQty(context)">修改数量</Button></SpaceItem>
                    </Space>
                </Space>

            </ActionColumn>
        </Table>
    </Body>
</Card>


<Modal Title="修改库存"
       Visible="@_editQtyVisible"
       OnOk="@EditQtyHandleOk"
       OnCancel="@(()=>_editQtyVisible=false)">
    <Text>@(current.Name)：</Text>
    <AntDesign.InputNumber @bind-Value="current.Qty" Min="0"></AntDesign.InputNumber>
    <Text>支</Text>
</Modal>

<Modal Title="添加疫苗"
       Visible="@_addVisible"
       OnOk="@AddHandleOk"
       OnCancel="@(()=>_addVisible=false)">
    <Form @ref="form"
          Model="@newModel"
          LabelColSpan="6"
          WrapperColSpan="18">
        <FormItem Label="疫苗名称">
            <Input @bind-Value="@context.Name" />
        </FormItem>
        <FormItem Label="疫苗描述">
            <Input @bind-Value="@context.Descr" />
        </FormItem>

        <FormItem Label="数量">
            <AntDesign.InputNumber @bind-Value="@context.Qty" Min="0" Style="width:100%;"></AntDesign.InputNumber>
        </FormItem>
    </Form>
</Modal>
@code {
    bool _editQtyVisible;
    bool _addVisible;
    VaccineDto current = new();
    bool loading = false;
    VaccineDto newModel = new();
    private Form<VaccineDto> form = new();

    /// <summary>
    /// 新建疫苗
    /// </summary>
    private void Add()
    {
        _addVisible = true;
    }
    /// <summary>
    /// 编辑数量
    /// </summary>
    private void EditQty(VaccineDto vaccineDto)
    {
        current = vaccineDto;
        _editQtyVisible = true;
    }

    /// <summary>
    /// 确认新增
    /// </summary>
    private void AddHandleOk()
    {
        if (form.Validate())
        {
            datas.Add(newModel);
            _editQtyVisible = false;
        }
    }

    /// <summary>
    /// 确认修改数量
    /// </summary>
    /// <param name="e"></param>
    private void EditQtyHandleOk(MouseEventArgs e)
    {
        Console.WriteLine(e);
        _editQtyVisible = false;
    }
    List<VaccineDto> datas = new List<VaccineDto>
                                                                 {
                    new()
                    {
                       Id=1,
                       Name="新型冠状病毒灭活疫苗1",
                       Descr="这只是疫苗描述",
                       Qty=1
                    },
                    new()
                    {
                       Id=1,
                       Name="新型冠状病毒灭活疫苗2",
                       Descr="这只是疫苗描述2",
                       Qty=1000
                    },
                                                                };

    public class VaccineDto
    {
        /// <summary>
        /// Id
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 疫苗名称
        /// </summary>
        [DisplayName("疫苗名称")]
        [Required(ErrorMessage = "请输入疫苗名称")]
        public string? Name { get; set; }

        /// <summary>
        /// 疫苗描述
        /// </summary>
        [DisplayName("描述")]
        public string? Descr { get; set; }

        /// <summary>
        /// 数量
        /// </summary>
        [DisplayName("数量(支")]
        public int Qty { get; set; }
    }
}